/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */

package team.mai.antexpress.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.mai.antexpress.dao.CenterDAO;
import team.mai.antexpress.dao.DelivermanDAO;
import team.mai.antexpress.dao.UserDAO;
import team.mai.antexpress.entities.CenterEntity;
import team.mai.antexpress.entities.DelivermanEntity;
import team.mai.antexpress.entities.DepartmentEntity;
import team.mai.antexpress.entities.PermissionGroupEntity;

import java.util.List;

@Service
public class CenterService {
	public static final int SUCCESS = 0;
	public static final int ERROR = -1;
	public static final int INSUFFICIENT_PRIVILEGE = -2;
	public static final int USER_IN_CENTER = -3;

	@Autowired
	private CenterDAO centerDAO;
	@Autowired
	private UserDAO userDAO;

	public int insertCenter(String operatorName, CenterEntity cent) {
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyCenter()) {
			return centerDAO.insertCenter(cent);
		} else {
			return INSUFFICIENT_PRIVILEGE;
		}
	}

	public int deleteCenter(String operatorName, int centId) {
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyCenter()) {
			if(centerDAO.hasUserInCenter(centId)){
				return USER_IN_CENTER;
			}
			if (centerDAO.deleteCenter(centId)) {
				return SUCCESS;
			} else {
				return ERROR;
			}

		} else {
			return INSUFFICIENT_PRIVILEGE;
		}
	}

	public int deleteUsersInCenter(String operatorName, int centId){
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyCenter()) {
			if (centerDAO.deleteUsersInCenter(centId)) {
				return SUCCESS;
			} else {
				return ERROR;
			}

		} else {
			return INSUFFICIENT_PRIVILEGE;
		}
	}

	@Deprecated
	public int deleteCenters(String operatorName, List<Integer> ids) {
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyDepartment()) {
			if (centerDAO.deleteCenters(ids)) {
				return SUCCESS;
			} else {
				return ERROR;
			}
		} else {
			return INSUFFICIENT_PRIVILEGE;
		}
	}

	public int updateCenter(String operatorName, CenterEntity del) {
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyCenter()) {
			if (centerDAO.updateCenter(del)) {
				return SUCCESS;
			} else {
				return ERROR;
			}
		} else {
			return INSUFFICIENT_PRIVILEGE;
		}

	}

	public List<CenterEntity> getAllCenters(String operatorName) {
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
		if (permissionByUser.isAdmin() || permissionByUser.isLookupCenter()) {
			return centerDAO.getAllCenters();
		} else {
			return null;
		}

	}

	public CenterEntity getCenter(String operatorName, Integer id) {
		if (id == null)
			return null;
		else {
			PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
			if (permissionByUser.isAdmin() || permissionByUser.isLookupCenter()) {
				return centerDAO.getCenter(id);
			} else {
				return null;
			}
		}
	}

	@Deprecated
	public CenterEntity getCenter(String operatorName, String delName) {
		if (delName == null) {
			return null;
		} else {
			PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
			if (permissionByUser.isAdmin() || permissionByUser.isLookupCenter()) {
				return centerDAO.getCenter(delName);
			} else {
				return null;
			}
		}
	}
}
